package com.sevenGroup.pawnshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sevenGroup.pawnshop.Utils.CopyProperties;
import com.sevenGroup.pawnshop.Utils.GetJson;
import com.sevenGroup.pawnshop.Utils.GetObjectByJson;
import com.sevenGroup.pawnshop.controller.form.FirstTypePropertiesForm;
import com.sevenGroup.pawnshop.controller.form.PropertiesForm;
import com.sevenGroup.pawnshop.controller.form.SelectPropertiesForm;
import com.sevenGroup.pawnshop.controller.form.pageForm;
import com.sevenGroup.pawnshop.controller.info.PropertiesInfo;
import com.sevenGroup.pawnshop.dao.FirstTypeDao;
import com.sevenGroup.pawnshop.dao.FirstTypePropertiesDao;
import com.sevenGroup.pawnshop.dao.GoodsPropertiesDao;
import com.sevenGroup.pawnshop.dao.PropertiesDao;
import com.sevenGroup.pawnshop.exception.sevenGroupException;
import com.sevenGroup.pawnshop.model.FirstType;
import com.sevenGroup.pawnshop.model.FirstTypeProperties;
import com.sevenGroup.pawnshop.model.GoodsProperties;
import com.sevenGroup.pawnshop.model.Properties;
import com.sevenGroup.pawnshop.service.PropertiesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-18
 */
@Service
public class PropertiesServiceImpl extends ServiceImpl<PropertiesDao, Properties> implements PropertiesService {

    @Resource
    private PropertiesDao propertiesDao;

    @Resource
    private FirstTypeDao firstTypeDao;

    @Resource
    private GoodsPropertiesDao goodsPropertiesDao;

    @Resource
    private FirstTypePropertiesDao firstTypePropertiesDao;

    @Override
    public void saveProperties(PropertiesForm propertiesForm) throws sevenGroupException {

        if (propertiesForm == null) {

            throw new sevenGroupException(40011, "参数为空");

        }
        //新增属性，需要将其中的集合转为json字符串存入数据库
        String json = GetJson.getListJson("", propertiesForm.getPropertiesValues());
        Properties properties = CopyProperties.copyOne(propertiesForm, Properties.class);
        properties.setPropertiesValue(json);
        int insert = propertiesDao.insert(properties);
        if (insert <= 0) {

            throw new sevenGroupException(40011, "新增失败");

        }
    }

    @Override
    public void updateProperties(PropertiesForm propertiesForm) throws sevenGroupException {
        if (propertiesForm == null) {

            throw new sevenGroupException(40016, "参数为空");
        }

        //更新属性，同样将属性集合取出转为json字符串
        String json = GetJson.getListJson("", propertiesForm.getPropertiesValues());
        Properties properties = CopyProperties.copyOne(propertiesForm, Properties.class);
        properties.setPropertiesValue(json);
        int i = propertiesDao.updateById(properties);
        if (i <= 0) {

            throw new sevenGroupException(40016, "更新失败");

        }
    }

    @Override
    public IPage selectAllProperties(pageForm pageForm) throws sevenGroupException {
        if (pageForm == null) {

            throw new sevenGroupException(40001, "参数为空");

        }
        //创建分页对象
        pageForm.setPageNum(pageForm.getPageNum() == null ? 1 : pageForm.getPageNum());
        pageForm.setPageSize(pageForm.getPageSize() == null ? 5 : pageForm.getPageSize());
        Page<Properties> propertiesPage = new Page<>(pageForm.getPageNum(), pageForm.getPageSize());

        //查询
        Page<Properties> selectPage = propertiesDao.selectPage(propertiesPage, null);

        if (selectPage == null) {

            throw new sevenGroupException(40014, "查询失败");

        }
        //new一个page对象，以便返回
        IPage propertiesInfoIPage = new Page<>();
        ArrayList<PropertiesInfo> propertiesInfos = new ArrayList<>();

        //循环selectPage，将其中的json对象转出来的集合设置到PropertiesInfo中
        for (Properties properties : selectPage.getRecords()) {
            List list = GetObjectByJson.getList("", properties.getPropertiesValue(), String.class);
            PropertiesInfo propertiesInfo = CopyProperties.copyOne(properties, PropertiesInfo.class);
            propertiesInfo.setPropertiesValues(list);
            propertiesInfos.add(propertiesInfo);
        }

        //设置propertiesInfoIPage参数
        propertiesInfoIPage.setRecords(propertiesInfos);
        propertiesInfoIPage.setCurrent(selectPage.getCurrent());
        propertiesInfoIPage.setSize(selectPage.getSize());
        propertiesInfoIPage.setTotal(selectPage.getTotal());

        return propertiesInfoIPage;
    }

    @Override
    public List<PropertiesInfo> selectPropertiesByGoodId(Integer goodId) throws sevenGroupException {

        if (goodId == null || goodId <= 0) {
            throw new sevenGroupException(40014, "参数异常");
        }

        QueryWrapper<GoodsProperties> wrapper = new QueryWrapper<>();
        wrapper.eq("good_id", goodId);
        //根据商品id查询出属性集合
        List<GoodsProperties> goodsProperties = goodsPropertiesDao.selectList(wrapper);

        if (goodsProperties == null) {
            throw new sevenGroupException(40014, "查询失败");
        }
        //新建一个集合，将查询出的属性id存入
        ArrayList<Integer> integers = new ArrayList<>();
        for (GoodsProperties goodsProperty : goodsProperties) {
            integers.add(goodsProperty.getPropertiesId());
        }
        if(integers==null||integers.size()<=0){
            throw new sevenGroupException(40030,"该商品没有属性");
        }
        QueryWrapper<Properties> wrapper1 = new QueryWrapper<>();
        wrapper1.in("properties_id", integers);
        List<Properties> properties = propertiesDao.selectList(wrapper1);
        if (properties == null) {

            throw new sevenGroupException(40019, "查询失败");

        }
        //新建PropertiesInfo集合，将查询出的参数设置进去，将json字符串转为属性集合
        ArrayList<PropertiesInfo> propertiesInfos = new ArrayList<>();
        for (Properties property : properties) {
            List list = GetObjectByJson.getList("", property.getPropertiesValue(), String.class);
            PropertiesInfo propertiesInfo = CopyProperties.copyOne(property, PropertiesInfo.class);
            propertiesInfo.setPropertiesValues(list);
            propertiesInfos.add(propertiesInfo);
        }

        return propertiesInfos;
    }

    @Override
    public IPage<PropertiesInfo> selectPropertiesByPropertiesName(SelectPropertiesForm selectPropertiesForm) throws sevenGroupException {

        selectPropertiesForm.setPageNum(selectPropertiesForm.getPageNum() == null ? 1 : selectPropertiesForm.getPageNum());
        selectPropertiesForm.setPageSize(selectPropertiesForm.getPageSize() == null ? 5 : selectPropertiesForm.getPageSize());
        Page<Properties> propertiesPage = new Page<>(selectPropertiesForm.getPageNum(), selectPropertiesForm.getPageSize());
        //模糊查询出所有的属性
        QueryWrapper<Properties> wrapper = new QueryWrapper<>();
        wrapper.like("properties_name", selectPropertiesForm.getPropertiesName());
        Page propertiesPage1 = propertiesDao.selectPage(propertiesPage, wrapper);
        if (propertiesPage1 == null) {

            throw new sevenGroupException(40019, "查询失败");

        }
        //新建PropertiesInfo集合并将参数设置进去
        ArrayList<PropertiesInfo> propertiesInfos = new ArrayList<>();
        for (Object record : propertiesPage1.getRecords()) {
            Properties properties = (Properties) record;
            List list = GetObjectByJson.getList("", properties.getPropertiesValue(), String.class);
            PropertiesInfo propertiesInfo = CopyProperties.copyOne(properties, PropertiesInfo.class);
            propertiesInfo.setPropertiesValues(list);
            propertiesInfos.add(propertiesInfo);
        }

        propertiesPage1.setRecords(propertiesInfos);

        return propertiesPage1;
    }


    @Override
    public List<PropertiesInfo> selectPropertiesByFirstTypeId(Integer firstTypeId) throws sevenGroupException {

        if (firstTypeId == null || firstTypeId <= 0) {
            throw new sevenGroupException(40014, "参数异常");
        }
        //先查询出中间表所有的数据
        List<FirstTypeProperties> firstTypeProperties = firstTypePropertiesDao.selectList(new QueryWrapper<FirstTypeProperties>().eq("first_type_id", firstTypeId));

        if (firstTypeProperties == null) {

            throw new sevenGroupException(40020, "查询失败");

        }
        //新建集合存入id
        ArrayList<Integer> integers = new ArrayList<>();
        for (FirstTypeProperties firstTypeProperty : firstTypeProperties) {
            integers.add(firstTypeProperty.getPropertiesId());
        }

        if (integers == null || integers.size() <= 0) {
            return null;
        }

        //根据id查询，并设置进去值
        ArrayList<PropertiesInfo> propertiesInfos = new ArrayList<>();

        List<Properties> list = propertiesDao.selectList(new QueryWrapper<Properties>().in("properties_id", integers));

        if (list == null) {

            throw new sevenGroupException(40020, "查询失败");

        }
        //转json字符串，class对象为要转的对象的字节码
        for (Properties properties : list) {
            List list1 = GetObjectByJson.getList("", properties.getPropertiesValue(), String.class);
            PropertiesInfo propertiesInfo = CopyProperties.copyOne(properties, PropertiesInfo.class);
            propertiesInfo.setPropertiesValues(list1);
            propertiesInfos.add(propertiesInfo);
        }
        return propertiesInfos;
    }

    @Override
    @Transactional
    public void updateFirstTypeProperties(FirstTypePropertiesForm firstTypePropertiesForm) throws sevenGroupException {
        if (firstTypePropertiesForm == null) {
            throw new sevenGroupException(40001, "参数为空");
        }
        //根据参数判断更新哪个中间表
        QueryWrapper<FirstTypeProperties> wrapper = new QueryWrapper<>();
        if (firstTypePropertiesForm.getFirstTypeId() != null && firstTypePropertiesForm.getFirstTypeId() > 0 && firstTypePropertiesForm.getPropertiesId() == null || firstTypePropertiesForm.getPropertiesId() < 0) {
            wrapper.eq("first_type_id", firstTypePropertiesForm.getFirstTypeId());
            firstTypePropertiesDao.delete(wrapper);

            for (Integer propertiesId : firstTypePropertiesForm.getPropertiesIds()) {
                FirstTypeProperties firstTypeProperties = FirstTypeProperties.builder().firstTypeId(firstTypePropertiesForm.getFirstTypeId()).propertiesId(propertiesId).build();
                firstTypePropertiesDao.insert(firstTypeProperties);
            }
        } else if (firstTypePropertiesForm.getPropertiesId() != null && firstTypePropertiesForm.getPropertiesId() > 0 && firstTypePropertiesForm.getFirstTypeId() == null || firstTypePropertiesForm.getFirstTypeId() < 0) {
            wrapper.eq("properties_id", firstTypePropertiesForm.getPropertiesId());
            firstTypePropertiesDao.delete(wrapper);

            for (Integer firstTypeId : firstTypePropertiesForm.getFirstTypeIds()) {
                FirstTypeProperties firstTypeProperties = FirstTypeProperties.builder().firstTypeId(firstTypeId).propertiesId(firstTypePropertiesForm.getPropertiesId()).build();
                firstTypePropertiesDao.insert(firstTypeProperties);
            }
        }
    }

    @Override
    public void delPropertiesByPropertiesId(Integer propertiesId) throws sevenGroupException {
        if (propertiesId == null || propertiesId <= 0) {
            throw new sevenGroupException(40014, "参数异常");
        }
        propertiesDao.deleteById(propertiesId);
        QueryWrapper<FirstTypeProperties> wrapper = new QueryWrapper<>();
        wrapper.eq("properties_id", propertiesId);
        firstTypePropertiesDao.delete(wrapper);
    }

    @Override
    public void delPropertiesByList(List<Integer> list) throws sevenGroupException {
        if (list == null || list.size() <= 0) {
            throw new sevenGroupException(4001, "参数异常");
        }

        propertiesDao.deleteBatchIds(list);
        QueryWrapper<FirstTypeProperties> wrapper = new QueryWrapper<>();
        wrapper.in("properties_id", list);
        firstTypePropertiesDao.delete(wrapper);
    }

    @Override
    public List<FirstType> selectFirstTypesByProperties(Integer propertiesId) throws sevenGroupException {
        if (propertiesId == null||propertiesId<=0) {
            throw new sevenGroupException(40001, "参数异常");
        }
        //先查询出对应的属性id集合
        QueryWrapper<FirstTypeProperties> wrapper = new QueryWrapper<>();
        wrapper.eq("properties_id", propertiesId);
        List<FirstTypeProperties> firstTypeProperties = firstTypePropertiesDao.selectList(wrapper);

        //新建集合存入id
        ArrayList<Integer> integers = new ArrayList<>();

        for (FirstTypeProperties firstTypeProperty : firstTypeProperties) {
            integers.add(firstTypeProperty.getFirstTypeId());
        }

        if (integers == null || integers.size() <= 0) {
            return null;
        }
        //根据id集合查询，并返回
        QueryWrapper<FirstType> wrapper1 = new QueryWrapper<>();
        wrapper1.in("first_type_id", integers);
        List<FirstType> firstTypes = firstTypeDao.selectList(wrapper1);

        return firstTypes;

    }
}
